package com.spring.boot.config.websocket.server;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.spring.boot.entity.*;
import com.spring.boot.response.Result;
import com.spring.boot.service.*;
import com.spring.boot.utils.MyUtils;
import com.spring.boot.vo.CityVo;
import com.spring.boot.vo.RadarVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

//@PreAuthorize("hasAnyAuthority('ROLE_bigScreen')")
//@ServerEndpoint(value = "/ws/{username}")
@Component
public class WebSocketServer {

    private final static Logger log = LoggerFactory.getLogger(WebSocketServer.class);

    @Autowired
    private SimpMessagingTemplate messagingTemplate;

    @Autowired
    private ProvinceService provinceService;
    @Autowired
    private CityService cityService;
    @Autowired
    private PredictionService predictionService;
    @Autowired
    private RankingService rankingService;
    @Autowired
    private RadarService radarService;
    @Autowired
    private ProvinceMapService provinceMapService;
    @Autowired
    private CityMapService cityMapService;
    @Autowired
    private InOutNumService inOutNumService;
    @Autowired
    private DangerLevelService dangerLevelService;
    @Autowired
    private ChinaCityService chinaCityService;
    @Autowired
    private SummaryService summaryService;

//    @Scheduled(fixedDelay = 5000)//spring的计划任务，这里表示每五秒执行一次，需要在配置类或者启动类上加一个注解@EnableScheduling，这样才能生效
//    public void mes() {
//        List<TitleDate> result = provinceService.listTitleDate(new QueryWrapper<Province>().orderByDesc("id"));
//        messagingTemplate.convertAndSend("/topic", result);//最好把查询结果放在程序上下文里，不然重复查询数据库可能会加大数据库压力
//    }

    @Scheduled(fixedDelay = 3000)
    public void province() {
        List<TitleDate> result = provinceService.listTitleDate("");
        messagingTemplate.convertAndSend("/bigScreen/province", result);
    }
    @Scheduled(fixedDelay = 3000)
    public void city(){
        CityVo result = cityService.getCityInfo(new QueryWrapper<ProvinceCity>());
        messagingTemplate.convertAndSend("/bigScreen/city", result);
    }
    @Scheduled(fixedDelay = 3000)
    public void ranking(){
        String date = "";
        List<?> result = rankingService.getRankingInfo(date);
        messagingTemplate.convertAndSend("/bigScreen/ranking", result);
    }

    @Scheduled(fixedDelay = 3000)
    public void radarData(){
        RadarVo result = radarService.getRadarData("");
        messagingTemplate.convertAndSend("/bigScreen/radarData", result);
    }
    @Scheduled(fixedDelay = 3000)
    public void provinceMapData(){
        List<Integer> result = provinceMapService.getProvinceMapData("");
        messagingTemplate.convertAndSend("/bigScreen/provinceMapData", result);
    }

    @Scheduled(fixedDelay = 3000)
    public void cityMapData() {
        List<Integer> result = cityMapService.getCityMapData();
        messagingTemplate.convertAndSend("/bigScreen/cityMapData", result);
    }

//    @Scheduled(fixedDelay = 3000)
//    public void predictionData() {
//        Map result = predictionService.getPredictionData();
//        messagingTemplate.convertAndSend("/bigScreen/predictionData", result);
//    }

    @Scheduled(fixedDelay = 3000)
    public void dangerLevel() {
        QueryWrapper<DangerLevel> qw = new QueryWrapper<>();
        qw.likeRight("date", MyUtils.today());
        DangerLevel result = dangerLevelService.getOne(qw);
        if (ObjectUtil.isNull(result)) {
            messagingTemplate.convertAndSend("/bigScreen/dangerLevel", Result.error("危险等级查询结果为空！"));
        } else {
            messagingTemplate.convertAndSend("/bigScreen/dangerLevel", result);
        }

    }

//    @Scheduled(fixedDelay = 3000)
//    public void trend() {
//        Map result = provinceService.getTrend();
//        messagingTemplate.convertAndSend("/bigScreen/trend", result);
//    }

    @Scheduled(fixedDelay = 3000)
    public void getCitiesTrend() {
        Map result = chinaCityService.getCitiesTrend();
        messagingTemplate.convertAndSend("/bigScreen/citiesTrend", result);
    }

    @Scheduled(fixedDelay = 3000)
    public void getFlyLineData() {
        List<InOutNum> result = inOutNumService.getFlyLineData(MyUtils.getYesterday());
        if (result.size() == 0) {
            messagingTemplate.convertAndSend("/bigScreen/flyLineData", Result.error("飞线数据获取异常！"));
        } else {
            messagingTemplate.convertAndSend("/bigScreen/flyLineData", result);
        }
    }

    @Scheduled(fixedDelay = 3000)
    public void getSummary() {
        QueryWrapper<Summary> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("create_time", MyUtils.today());
        List<Summary> result = summaryService.list(queryWrapper);
        messagingTemplate.convertAndSend("/bigScreen/summary", result);
    }

}

